import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { createPinia } from 'pinia'
import axios from "axios"
import 'element-plus/theme-chalk/index.css';
import "./assets/font/iconfont.css"

import {useLoginStore} from "./store"
router.beforeEach(async (to,from)=>{
	if(to.meta.requiredLogin){
		if(!useLoginStore().userInfo)
			return{
				path:"/login",
				query:{
					redirect:to.fullPath
				}
			}
	}
})

axios.interceptors.request.use(config=>{
	//如果状态数据包含token,头部添加token
	let token=useLoginStore().token;//保存的token已包含'Bearer '
	if(token){
		config.headers['Authorization']=token;
	}
	return config;
})

axios.interceptors.response.use(config=>{
	let AutoLogin=config.headers['autologin'];
	if(AutoLogin){
		useLoginStore().logout();
		setTimeout(()=>{
			router.push(AutoLogin)
		},3000)
	}
	return config;
})

const app=createApp(App);
app.config.globalProperties.$axios=axios;

app.use(createPinia()).use(router).mount('#app')
